package com.dhcc.rc.controller;
 
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dhcc.rc.entity.User;
import com.dhcc.rc.entity.vo.PageResult;
import com.dhcc.rc.entity.vo.Result;
import com.dhcc.rc.entity.vo.ResultCode;
import com.dhcc.rc.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
 
@RestController
@RequestMapping("/")
@Api(tags = "用户模块")
public class UserController {
 
    @Autowired
    private UserService userService;
    
    @ApiOperation(value="分页查询",nickname="用户模块")
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public Result findAll(int page, int size, @RequestParam Map map) {
        //1.完成查询
        Page<User> pageUser = userService.findAll(map,page,size);
        //2.构造返回结果
        PageResult pageResult = new PageResult(pageUser.getTotalElements(),pageUser.getContent());
        return new Result(ResultCode.SUCCESS, pageResult);
    }
    
    @ApiOperation(value="查询用户集合",nickname="用户模块")
    @RequestMapping(value = "/user/findlist", method = RequestMethod.GET)
    public Result findAll() {
        //1.完成查询
    	List<User> userList = userService.findAll();
        //2.构造返回结果
        return new Result(ResultCode.SUCCESS, userList);
    }
    /**
     * 根据ID查询user
     */
    @ApiOperation(value="根据用户id查询",nickname="用户模块")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable(value = "id") String id) {
        User user = userService.findById(id);
        return new Result(ResultCode.SUCCESS, user);
    }
    /**
     * 根据ID查询user
     */
    @ApiOperation(value="根据用户findOne查询",nickname="用户模块")
    @RequestMapping(value = "/user/findOne/{id}", method = RequestMethod.GET)
    public Result findOne(@PathVariable(value = "id") String id) {
    	User user = userService.findOne(id);
    	return new Result(ResultCode.SUCCESS, user);
    }
    @ApiOperation(value="根据用户findTwo查询",nickname="用户模块")
    @RequestMapping(value = "/user/findTwo/{id}", method = RequestMethod.GET)
    public Result findTwo(@PathVariable(value = "id") String id) {
    	User user = userService.findTwo(id);
    	return new Result(ResultCode.SUCCESS, user);
    }
    @ApiOperation(value="根据用户findThree查询",nickname="用户模块")
    @RequestMapping(value = "/user/findThree/{id}", method = RequestMethod.GET)
    public Result findThree(@PathVariable(value = "id") String id) {
    	User user = userService.findThree(id);
    	return new Result(ResultCode.SUCCESS, user);
    }
    @ApiOperation(value="根据用户findFour查询",nickname="用户模块")
    @RequestMapping(value = "/user/findFour/{id}", method = RequestMethod.GET)
    public Result findFour(@PathVariable(value = "id") String id) {
    	User user = userService.findFour(id);
    	return new Result(ResultCode.SUCCESS, user);
    }
    /**
     * 保存User
     */
    @ApiOperation(value="用户保存",nickname="用户模块")
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public Result save(@RequestBody User user) {
        //1.调用service完成保存
        userService.save(user);
        //2.构造返回结果
        return new Result(ResultCode.SUCCESS);
    }
    /**
     * 修改User
     */
    @ApiOperation(value="用户更新",nickname="用户模块")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable(value = "id") String id, @RequestBody User user) {
        //1.设置修改的部门id
        user.setId(id);
        //2.调用service更新
        userService.update(user);
        return new Result(ResultCode.SUCCESS);
    }
    /**
     * 根据id删除
     */
    @ApiOperation(value="用户删除",nickname="用户模块")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") String id) {
        userService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }
 
}